
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;

namespace Tathata.Web.Administration
{
    public partial class Administration_CategoryAdd : BaseNopAdministrationPage
    {
        private void BindData()
        {
            ParentCategory.SelectedCategoryId = this.ParentCategoryID;
            ParentCategory.BindData();
        }

        private void FillDropDowns()
        {
            this.ddlTemplate.Items.Clear();
            CategoryTemplateCollection categoryTemplates = CategoryTemplateManager.GetAllCategoryTemplates();
            foreach (CategoryTemplate categoryTemplate in categoryTemplates)
            {
                ListItem item2 = new ListItem(categoryTemplate.Name, categoryTemplate.CategoryTemplateID.ToString());
                this.ddlTemplate.Items.Add(item2);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.FillDropDowns();
                this.BindData();
            }
        }

        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Picture categoryPicture = null;
                    HttpPostedFile categoryPictureFile = fuCategoryPicture.PostedFile;
                    if ((categoryPictureFile != null) && (!String.IsNullOrEmpty(categoryPictureFile.FileName)))
                    {
                        byte[] categoryPictureBinary = PictureManager.GetPictureBits(categoryPictureFile.InputStream, categoryPictureFile.ContentLength);
                        categoryPicture = PictureManager.InsertPicture(categoryPictureBinary, categoryPictureFile.ContentType, true);
                    }
                    int categoryPictureID = 0;
                    if (categoryPicture != null)
                        categoryPictureID = categoryPicture.PictureID;

                    DateTime nowDT = DateTime.Now;
                    Category category = CategoryManager.InsertCategory(txtName.Text, txtDescription.Value, int.Parse(this.ddlTemplate.SelectedItem.Value),
                             txtMetaKeywords.Text, txtMetaDescription.Text, txtMetaTitle.Text, txtSEName.Text, ParentCategory.SelectedCategoryId,
                             categoryPictureID, cbPublished.Checked, false, txtDisplayOrder.Value, nowDT, nowDT);

                    Response.Redirect(string.Format("CategoryDetails.aspx?CategoryID={0}", category.CategoryID));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }

        public int ParentCategoryID
        {
            get
            {
                return CommonHelper.QueryStringInt("ParentCategoryID");
            }
        }
    }
}
